package com.osdp.uc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.osdp.common.exception.PlatformException;
import com.osdp.common.util.ConvertUtil;
import com.osdp.uc.entity.UcOrganization;
import com.osdp.uc.service.IUcOrganizationService;
import com.osdp.uc.vo.UcOrganizationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/uc/organization")
@Api("菜单权限管理")
public class UcOrganizationController {

    @Autowired
    IUcOrganizationService organizationService;

    /**
     * 异步查询，点击加载数据
     * @param parentId
     * @return
     */
    @RequestMapping(value = "/queryListByParentId", method = RequestMethod.GET)
    @ApiOperation(value = "异步加载权限树", tags = "异步加载权限树")
    public List<UcOrganizationVo> queryListByParentId(
            @RequestParam(name = "parentId", required = true) String parentId,
            @RequestParam(name = "orgTypeId", required = false) String orgTypeId){
        List<UcOrganization> list =  organizationService.queryByParentId(parentId,orgTypeId);
        return ConvertUtil.sourceToTarget(list, UcOrganizationVo.class);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.GET)
    public IPage<UcOrganizationVo> pageList(
            UcOrganizationVo organizationVo,
            @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize){
        return organizationService.pageList(organizationVo,new Page<>(pageNo, pageSize));
    }

    @PostMapping("/insert")
    public UcOrganizationVo insertOrganization(@RequestBody UcOrganizationVo organizationVo){
        return organizationService.insertOrganization(organizationVo);
    }

    @PutMapping("/update")
    public UcOrganizationVo updateOrganization(@RequestBody UcOrganizationVo organizationVo){
        return organizationService.updateOrganization(organizationVo);
    }

    @DeleteMapping("/delete")
    public boolean deleteOrganization(@RequestParam(name = "id", required = true) String id){
        if(StringUtils.isEmpty(id)) {
            throw new PlatformException("组织机构Id不能为空！");
        }
        return organizationService.removeById(id);
    }
    @GetMapping("/queryById")
    public UcOrganizationVo queryById(@RequestParam(name = "id", required = true)String id){
        UcOrganization organization= this.organizationService.getById(id);
        return ConvertUtil.sourceToTarget(organization,UcOrganizationVo.class);
    }
}
